
package com.younion.util;

/**
 * @author wwh
 */
public class HtmlIOUtil {
	
	
	public static String filterForHtmlBR(String value)
	{
		//System.out.println("-------filter begin: " + value);
		if (value == null)
			return ("");

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
				/*
				case ' ' :
					result.append("&nbsp;");
					break;
				*/
				case 13 :
					break;
				case 10 :
					result.append("<br>");
					break;
				default :
					result.append(content[i]);
			}
		}
		return (result.toString());		
	}
	
	public static String filterForHtml(String value) {
		//System.out.println("-------filter begin: " + value);
		if (value == null)
			return ("");

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			//System.out.println("-------char[" + i + "] : " + (int) content[i]);
			//System.out.println("-------char[" + i + "] : " + content[i]);
			switch (content[i]) {
				case '<' :
					result.append("&lt;");
					break;
				case '>' :
					result.append("&gt;");
					break;
				case '&' :
					result.append("&amp;");
					break;
				case '"' :
					result.append("&quot;");
					break;
				case '\'' :
					result.append("&#39;");
					break;
				case ' ' :
					result.append("&nbsp;");
					break;
				case 13 :
					break;
				case 10 :
					result.append("<br>");
					break;
				default :
					result.append(content[i]);
			}
		}
		//System.out.println("-------filter end! ");
		return (result.toString());
	}

	public static String filterForScript(String value) {
		if (value == null)
			return ("");

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
				case '\\' :
					result.append("\\\\");
					break;	
				case '/' :
					result.append("\\/");
					break;									
				case '"' :
					result.append("\\\"");
					break;
				case '\'' :
					result.append("\\\'");
					break;
				case 13 :
					break;
				case 10 :
					result.append("\\n");
					break;
				default :
					result.append(content[i]);
			}
		}
		return (result.toString());
	}

	public static String filterForInput(String value) {
		if (value == null)
			return ("");

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
				case '\\' :
					result.append("\\\\");
					break;
				case '"' :
					result.append("\\\"");
					break;
				case 13 :
					result.append("\\\n");
					i++;
					break;
				default :
					result.append(content[i]);
			}
		}
		return (result.toString());
	}
}
